<template>

  <UploadExcel :on-success="upload" />
</template>

<script>
import { inputemployeesList } from '@/api/employees'
export default {
  data() {
    return {
      list: []
    }
  },
  methods: {
    async upload(data) {
      // console.log(123)
      const oldData = data.results
      const newData = oldData.map(user => {
        const newUser = this.EnToZh(user)
        return newUser
      })
      // console.log(newData)
      // try {
      const res = await inputemployeesList(newData)
      console.log(res)
      //   this.$message.success('导入成功')
      //   this.$router.back()
      // } catch (error) {
      //   console.log(error)
      // }
    },
    EnToZh(user) {
      const dict = {
        '入职日期': 'timeOfEntry',
        '姓名': 'username',
        '工号': 'workNumber',
        '手机号': 'mobile',
        '转正日期': 'correctionTime'
      }
      // 新用户对象
      const newUser = {}
      for (const key in user) {
        const enkey = dict[key]
        const value = user[key]
        // console.log(enkey, value)
        if (enkey === 'timeOfEntry' || enkey === 'correctionTime') {
          newUser[enkey] = new Date(this.formatExcelDate(value, '/'))

          // console.log(newUser[enkey])
        } else {
          newUser[enkey] = value
        }
      }
      return newUser
    },
    // excel读取2020/04/03 14:01这种时间格式是会将它装换成数字类似于3924.584027778 numb是传过来的整数数字，format是之间间隔的符号
    formatExcelDate(numb, format) {
      const time = new Date((numb - 1) * 24 * 3600000 + 1)
      time.setYear(time.getFullYear() - 70)
      time.setHours(time.getHours() - 8)
      const year = time.getFullYear() + ''
      const month = time.getMonth() + 1 + ''
      const date = time.getDate() + ''
      // const hours = time.getHours().toLocaleString()
      // const minutes = time.getMinutes()
      if (format && format.length === 1) {
        return year + format + month + format + date
      }
      return year + (month < 10 ? '0' + month : month) + (date < 10 ? '0' + date : date)
    }
  }
}
</script>
<style lang='less' scoped>

</style>
